<template>
  <div id="recorddialog">
    <commonformdialog :isshowDialogs="isshowDialog" :isshowconfirmbtn="false" :dialogWidth="'25%'" :enterTitle="false"
      :isshowimportTitle="false" :dialogTitle="`录入成绩-${titlename}`" @closetextDialog="closetextDialog" @confirmbtn='confirmbtn'
      >

      <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="80px">
        <el-form-item label="选择科目" prop="subject_type">
          <el-select v-model="formData.subject_type" placeholder="请选择选择科目" clearable :style="{width: '100%'}">
            <el-option v-for="(item, index) in subOption" :key="index" :label="item.label" :value="item.value"
              :disabled="item.disabled"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="录入成绩" prop="exam_result">
          <el-input v-model="formData.exam_result" placeholder="请输入成绩" clearable :style="{width: '100%'}">
            <template slot="append">分</template>
          </el-input>
        </el-form-item>
        <el-form-item label="考试时间" prop="exam_time">
          <el-date-picker v-model="formData.exam_time" format="yyyy-MM-dd" value-format="yyyy-MM-dd" :style="{width: '100%'}"
            placeholder="请选择日期选择" clearable></el-date-picker>
        </el-form-item>
      </el-form>

    </commonformdialog>
  </div>
</template>

<script>
  //引入弹窗
  import commonformdialog from '@/components/commondialog/commondialog'
  //api
  import {achievementcreate} from '@/api/student'
  export default {
    data() {
      return {
        //是否禁用科目选项
        issubdisable:false,
        //标题名字
        titlename: '',
        //学员id
        studentId: '',
        //表格数据
        formData: {
          subject_type: '',
          exam_result: '',
          exam_time: ''
        },
        rules: {
          subject_type: [{
            required: true,
            message: '请选择选择科目',
            trigger: 'change'
          }],
          exam_result: [{
            required: true,
            message: '请输入成绩',
            trigger: 'blur'
          }],
          exam_time: [{
            required: true,
            message: '请选择考试时间',
            trigger: 'blur'
          }]
        },
        subOption: [{
          "label": "科目一",
          "value": 1,
          "disabled":true
        }, {
          "label": "科目二",
          "value": 2,
          "disabled":true
        }, {
          "label": "科目三",
          "value": 3,
          "disabled":true
        }, {
          "label": "科目四",
          "value": 4,
          "disabled":true
        }],
      }
    },
    components: {
      commonformdialog,
    },
    props: {
      //是否显示弹窗
      isshowDialog: {
        type: Boolean,
        default: false,
      },
      //点击传递的参数
      dialogparams: {
        type: Object,
        default: () => {
          return {}
        }
      }
    },
    methods: {
      //关闭弹窗
      closetextDialog() {
        this.$refs['elForm'].resetFields()
        this.subOption.forEach((item,index)=>{
            item.disabled = true
        })
        this.$emit('closetextDialog')
      },
      //点击确认
      confirmbtn() {
        this.$refs['elForm'].validate( async valid => {
          if (!valid) return
          let params = {
            student_id:this.studentId,
            subject_type:this.formData.subject_type,
            exam_time:this.formData.exam_time,
            exam_result:this.formData.exam_result
          }
          let data = await achievementcreate(params)
          if(data.code=='0000'){
             this.$message.success('成绩录入成功！')
             this.$emit('confirm')
             this.subOption.forEach((item,index)=>{
                 item.disabled = true
             })
             this.$refs['elForm'].resetFields()
          }
        })

      }
    },
    watch: {
      dialogparams(val) {
        // //科目一分数
        let scoreone = parseInt(val.scoreone)?parseInt(val.scoreone):0;
        //科目二分数
        let scoretwo = parseInt(val.scoretwo)?parseInt(val.scoretwo):0;
        //科目三分数
        let scorethree = parseInt(val.scorethree)?parseInt(val.scorethree):0;
        //科目四分数
        let scorefour = parseInt(val.scorefour)?parseInt(val.scorefour):0;

        console.log(scoreone,scoretwo,scorethree,scorefour)
        //如果科目一的分数 小于90分 科目三 90及格
        if(scoreone<90||!scoreone){
          // 科目选项禁用
          this.$set(this.subOption[0],'disabled',false)
        }

        if(scoretwo<80&&scoreone>=90){

          this.$set(this.subOption[1],'disabled',false)
        }

        if(scorethree<90&&scoreone>=90){
           this.$set(this.subOption[2],'disabled',false)
        }

        if(scorefour<90&&scorethree>=90){
           this.$set(this.subOption[3],'disabled',false)
        }

        this.titlename = val.name
        this.studentId = val.id
      }
    }
  }
</script>

<style scoped lang='scss'>

</style>
